package edu.hawaii.ics211.exam;

//import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;

/**
 * Presents the user with an exam for ICS 211.
 * 
 * @author Branden Ogata
 *
 */

public class RunExam
{
  /**
   * The window in which to display the exam.
   * 
   */
  private static JFrame frame; 
  
  /**
   * The menu bar for the main window.
   * 
   */
  private static JMenuBar menu;
  
  /**
   * The File menu.
   * 
   */
  private static JMenu file;
  
  /**
   * The New Exam menu item.
   * 
   */
  private static JMenuItem newExam;
  
  /**
   * The Exit menu item.
   * 
   */
  private static JMenuItem exit;
  
  /**
   * The Help menu.
   * 
   */
  private static JMenu help;
  
  /**
   * The About menu item.
   * 
   */
  private static JMenuItem about;
  
  /**
   * The current exam questions.
   * 
   */
  private static List<ExamQuestion> exam;
  
  /**
   * Creates an instance of the exam for the user.
   * 
   * @param args A String[] to handle command line arguments; are there are none, this does nothing.
   * 
   */
  
  public static void main(String[] args)
  {
    // Create the frame
    frame = new JFrame("Sample Exam");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//    frame.setPreferredSize(new Dimension(1600, 800));

    // Add the menus
    MenuListener listener = new MenuListener();
    menu = new JMenuBar();
    file = new JMenu("File");
    newExam = new JMenuItem("New Exam");
    newExam.addActionListener(listener);
    exit = new JMenuItem("Exit");
    exit.addActionListener(listener);
    file.add(newExam);
    file.add(exit);
    menu.add(file);
    
    help = new JMenu("Help");
    about = new JMenuItem("About");
    about.addActionListener(listener);
    help.add(about);
    menu.add(help);
    
    frame.setJMenuBar(menu);
    
    // Add the exam itself
    exam = new ArrayList<ExamQuestion>();
    exam.addAll(GenerateExam.makeConditionalQuestions());
    exam.addAll(GenerateExam.makeLoopQuestions());
    exam.addAll(GenerateExam.makeArrayQuestions());
    exam.addAll(GenerateExam.makeRecursionQuestions());
    exam.addAll(GenerateExam.makeStackQuestions());
    exam.addAll(GenerateExam.makeQueueQuestions());
    exam.addAll(GenerateExam.makeTreeQuestions());
    exam.addAll(GenerateExam.makeSortQuestions());
    exam.addAll(GenerateExam.makeSetQuestions());
    exam.addAll(GenerateExam.makeMapQuestions());
    frame.getContentPane().add(new ExamViewer(exam));
    
    frame.pack();
    frame.setVisible(true);
  }

  /**
   * Listens for any actions on the menu.
   * 
   */
  
  private static class MenuListener implements ActionListener
  {
    /**
     * Handles any menu selections that do occur.
     * 
     * @param event The ActionEvent that recorded the menu action.
     * 
     */
    
    @Override
    public void actionPerformed(ActionEvent event)
    {
      // If the source was the New Exam item
      if (event.getSource() == newExam)
      {
        // For right now, just create a new exam; 
        // in future versions, this should open up a dialog to create a customized exam
        GenerateExam.reset();
        exam = new ArrayList<ExamQuestion>();
        exam.addAll(GenerateExam.makeConditionalQuestions());
        exam.addAll(GenerateExam.makeLoopQuestions());
        exam.addAll(GenerateExam.makeArrayQuestions());
        exam.addAll(GenerateExam.makeRecursionQuestions());
        exam.addAll(GenerateExam.makeStackQuestions());
        exam.addAll(GenerateExam.makeQueueQuestions());
        exam.addAll(GenerateExam.makeTreeQuestions());
        exam.addAll(GenerateExam.makeSortQuestions());
        exam.addAll(GenerateExam.makeSetQuestions());
        exam.addAll(GenerateExam.makeMapQuestions());
        frame.setContentPane(new ExamViewer(exam));
        frame.validate();
      }
      
      // Else if the source was the Exit item
      else if (event.getSource() == exit)
      {
        frame.dispose();
      }
      
      // Else if the source was the About item
      else if (event.getSource() == about)
      {
        String message = "";
        message += "ICS 211 Practice Exam\n";
        message += "\u00A9 2012 Branden Ogata \n";
        message += "\n";
        message += "TreeNode.java, HuffmanNode.java, syllabus.txt written by Zach Tomaszewski\n";
        message += "\n";
        message += "Special thanks to:\n";
        message += "<html><body><ul>";
        message += "  <li>Sean Mosier and Kristen Nakasone for their work ";
        message +=       "in testing the original ICS 111 Practice Exam</li>";
        message += "  <li>Sam Yuki for her work in testing the unoriginal ";
        message +=       "ICS 211 Practice Exam</li>";
        message += "  <li>BJ DeLaCruz and Pauline Wu for their advice regarding ";
        message +=       "the design and layout of the application</li>";
        message += "  <li>Doctors Martha Crosby and Michael Brian Ogawa for giving this ";
        message +=       "developer the opportunity to serve as a lab assistant first in ICS 111, ";
        message +=       "then in ICS 211</li>";
        message += "  <li>Doctors Scott Robertson and Zach Tomaszewski, ";
        message +=       "Robert Ward and Anthony Christe for generally tolerating the existence ";
        message +=       "of this developer</li>";
        message += "</ul></body></html>";
        message += "\n";
        message += "Dedicated to Kristen Nishihara and Lori Travis nee Ladrillono, ";
        message += "my first students in Computer Science.\n";
        message += "\n";

        JOptionPane.showMessageDialog(null, message, "About the Practice Exam", 
                                      JOptionPane.INFORMATION_MESSAGE);
      }
    }
    
  }
}
